package com.kfm.jdbc.day0406;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.UUID;

public class UploadServer {

    public static void main(String[] args) {
        try {
            ServerSocket server = new ServerSocket(8888);
            int count = 0;

            while (true){
                Socket accept = server.accept();

                System.out.println("有客户端连接" + accept.getInetAddress().getHostAddress());

                // 读取客户端发送的数据
                int length = 0;
                byte[] bytes = new byte[1024 * 1024 * 100];
                InputStream inputStream = accept.getInputStream();

                FileOutputStream fos = new FileOutputStream("d:\\temp\\" + count ++  + ".jpg");
                while( (length = inputStream.read(bytes)) != -1) {
                    fos.write(bytes, 0, length);
                }

                fos.close();
                System.out.println("上传成功");

                accept.getOutputStream().write("上传成功".getBytes());

                accept.close();
            }

        } catch (IOException e) {
            throw new RuntimeException(e);
        }


    }
}
